package com.tencent.cloud.uikit.ui.widget;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.uikit.R;

/**
 * 带状态图标的 Toast 布局
 *
 * @author showxiao
 * @date 2018/8/18
 */
public class StateToast extends LinearLayout {

    private static Toast sStateToast;

    public static void show(@NonNull Context context, CharSequence text) {
        show(context, text, StateToast.INFO);
    }

    public static void show(@NonNull Context context, CharSequence text, /*@ToastState */int state) {
        context = context.getApplicationContext();

        if (sStateToast != null) {
            sStateToast.cancel();
        }

        sStateToast = new Toast(context);
        StateToast stateToast =
                (StateToast) LayoutInflater.from(context).inflate(R.layout.state_toast, null);
        stateToast.setState(state);
        stateToast.setText(text);

        sStateToast.setGravity(Gravity.TOP, 0,
                ResourcesUtils.getDimensionPixelOffset(R.dimen.state_toast_offset_y)
        );
        sStateToast.setView(stateToast);
        sStateToast.setDuration(Toast.LENGTH_LONG);
        sStateToast.show();
    }

    public static final int RIGHT = 1;
    public static final int INFO = 0;
    public static final int ERROR = -1;

    @IntDef({RIGHT, INFO, ERROR})
    @interface ToastState {

    }

    private ImageView mIconView;
    private TextView mTextView;

    public StateToast(Context context) {
        super(context);
    }

    public StateToast(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public StateToast(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mIconView = findViewById(R.id.iv_state_icon);
        mTextView = findViewById(R.id.tv_state_text);
    }

    public void setIcon(@DrawableRes int iconResId) {
        mIconView.setImageResource(iconResId);
    }

    public void setText(@StringRes int textResId) {
        mTextView.setText(textResId);
    }

    public void setText(CharSequence text) {
        mTextView.setText(text);
    }

    public void setState(@ToastState int state) {
        switch (state) {
            case RIGHT:
                mIconView.setImageResource(R.drawable.tips_img_right);
                break;
            case INFO:
                mIconView.setImageResource(R.drawable.tips_img_nor);
                break;
            case ERROR:
                mIconView.setImageResource(R.drawable.tips_img_eorr);
                break;
            default:
                break;
        }
    }
}
